//
//  HMWelcomeViewController.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/1/30.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import SDWebImage

class HMWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = bgImage
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()

    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        headImage.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        messageLabel.alpha = 0
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                       NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootVcNotification, object: self)
                })
        }
    }
    
    private func setupUI() {
        view.addSubview(headImage)
        view.addSubview(messageLabel)
        headImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage)
            make.top.equalTo(headImage.snp_bottom).offset(15)
        }
    }

    
    // MARK: - 懒加载控件
    private lazy var bgImage: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    
    private lazy var headImage: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        if let imgUrl = HMUserAccountViewModel.sharedAccount.userAccount?.avatar_large {
           imageView.sd_setImageWithURL(NSURL(string: imgUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        imageView.cornerRadius = 45
        return imageView
    }()
    
    private lazy var messageLabel: UILabel = {
        let label =  UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(18)
        label.text = "欢迎回来"
        label.sizeToFit()
        return label
    }()

}
